/*
 *
 * Copyright(c) 2018-2019 chj <1019487871@qq.com>
 * QQ: 1019487871
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#include "los.h"

void
mach_gpio_af_set(GPIO_TypeDef* GPIOx,uint8_t BITx,uint8_t AFx)
{
    GPIOx->AFR[BITx>>3]&=~(0X0F<<((BITx&0X07)*4));
    GPIOx->AFR[BITx>>3]|=(uint32_t)AFx<<((BITx&0X07)*4);
}

void
mach_gpio_set(GPIO_TypeDef* GPIOx,uint32_t BITx,uint32_t MODE,uint32_t OTYPE,uint32_t OSPEED,uint32_t PUPD)
{
    uint32_t pinpos=0,pos=0,curpin=0;
    for(pinpos=0;pinpos<16;pinpos++)
    {
        pos=1<<pinpos;
        curpin=BITx&pos;
        if(curpin==pos)
        {
            GPIOx->MODER&=~(3<<(pinpos*2));
            GPIOx->MODER|=MODE<<(pinpos*2);
            if((MODE==0X01)||(MODE==0X02))
            {
                GPIOx->OSPEEDR&=~(3<<(pinpos*2));
                GPIOx->OSPEEDR|=(OSPEED<<(pinpos*2));
                GPIOx->OTYPER&=~(1<<pinpos) ;
                GPIOx->OTYPER|=OTYPE<<pinpos;
            }
            GPIOx->PUPDR&=~(3<<(pinpos*2));
            GPIOx->PUPDR|=PUPD<<(pinpos*2);
        }
    }
}



